﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;
    using WMS6.Web.CommonMethodServiceRef;

    public class PutAwayRuleDetail : BasePage
    {
        protected Button btnCancel;
        protected Button btnSave;
        protected Button btnSaveContinue;
        protected CheckBox chbDimRestrictions01;
        protected CheckBox chbDimRestrictions02;
        protected CheckBox chbDimRestrictions03;
        protected CheckBox chbDimRestrictions04;
        protected CheckBox chbDimRestrictions05;
        protected CheckBox chbDimRestrictions06;
        protected CheckBox chbIsActive;
        protected CheckBox chbLocStateRestriction01;
        protected CheckBox chbLocStateRestriction02;
        protected CheckBox chbLocStateRestriction03;
        protected CheckBox chbLocStateRestriction04;
        protected CheckBox chbLocStateRestriction05;
        protected DropDownList ddlStrategies;
        protected HtmlForm F2173;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdntype;
        protected HtmlImage imgFromLoc;
        protected HtmlImage imgToLoc;
        protected HtmlImage imgZone;
        protected Label lblAreaDetail;
        protected Label lblDimRestrictions;
        protected Label lblFromLoc;
        protected Label lblFromLocStar;
        protected Label lblLineID;
        protected Label lblLocSel;
        protected Label lblLocStateRestriction;
        protected Label lblMsg;
        protected Label lblPutawayRuleID;
        protected Label lblStrategies;
        protected Label lblToLoc;
        protected Label lblToLocStar;
        protected Label lblZone;
        protected Label lblZoneStar;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        private List<SysCode4DDL> sysCodeLocState;
        private List<SysCode4DDL> sysLocSel;
        private List<SysCode4DDL> sysPTDimRestrictions;
        private List<SysCode4DDL> sysPTStrategies;
        protected TextBox txtFromLoc;
        protected TextBox txtLineID;
        protected TextBox txtLocSel;
        protected TextBox txtPutawayRuleID;
        protected TextBox txtToLoc;
        protected TextBox txtZone;

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            base.Server.Transfer(string.Format("PutAwayRule.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "1", base.UrlEncode(this.txtPutawayRuleID.Text.Trim())));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool success = false;
            try
            {
                if (this.lblFromLocStar.Visible && string.IsNullOrEmpty(this.txtFromLoc.Text.Trim()))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F2410_007");
                    return;
                }
                if (this.lblToLocStar.Visible && string.IsNullOrEmpty(this.txtToLoc.Text.Trim()))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F2410_008");
                    return;
                }
                if (this.lblZoneStar.Visible && string.IsNullOrEmpty(this.txtZone.Text.Trim()))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F2410_009");
                    return;
                }
                PutawayRuleDetailInfo putawayRuleDetail = new PutawayRuleDetailInfo {
                    LineID = int.Parse(this.txtLineID.Text.Trim()),
                    PutawayRuleID = this.txtPutawayRuleID.Text.Trim(),
                    FromLoc = this.txtFromLoc.Text.Trim(),
                    ToLoc = this.txtToLoc.Text.Trim(),
                    Zone = this.txtZone.Text.Trim(),
                    DimRestriction01 = this.chbDimRestrictions01.Checked ? "10" : "",
                    DimRestriction02 = this.chbDimRestrictions02.Checked ? "20" : "",
                    DimRestriction03 = this.chbDimRestrictions03.Checked ? "30" : "",
                    DimRestriction04 = this.chbDimRestrictions04.Checked ? "40" : "",
                    DimRestriction05 = this.chbDimRestrictions05.Checked ? "50" : "",
                    DimRestriction06 = this.chbDimRestrictions06.Checked ? "60" : "",
                    LocStateRestriction01 = this.chbLocStateRestriction01.Checked ? "10" : "",
                    LocStateRestriction02 = this.chbLocStateRestriction02.Checked ? "20" : "",
                    LocStateRestriction03 = this.chbLocStateRestriction03.Checked ? "30" : "",
                    LocStateRestriction04 = this.chbLocStateRestriction04.Checked ? "40" : "",
                    LocStateRestriction05 = this.chbLocStateRestriction05.Checked ? "50" : "",
                    IsActive = this.chbIsActive.Checked ? "Y" : "N",
                    LocSelection = this.txtLocSel.Text,
                    PaStrategy = this.ddlStrategies.SelectedValue,
                    CreatedBy = base.UserInfo.UserID
                };
                if (!string.IsNullOrEmpty(this.hdntype.Value) && (this.hdntype.Value == "edit"))
                {
                    putawayRuleDetail.UpdatedBy = base.UserInfo.UserID;
                    if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().UpdateDetail(putawayRuleDetail, "PUTAWAY_RULE_DETAIL")))
                    {
                        success = true;
                    }
                }
                else if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(putawayRuleDetail, "PUTAWAY_RULE_DETAIL")))
                {
                    success = true;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2410_006") + ex.Message;
                return;
            }
            if (success)
            {
                base.Response.Redirect(string.Format("PutAwayRule.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "1", base.UrlEncode(this.txtPutawayRuleID.Text.Trim())));
            }
        }

        protected void btnSaveContinue_Click(object sender, EventArgs e)
        {
            bool success = false;
            try
            {
                PutawayRuleDetailInfo putawayRuleDetail = new PutawayRuleDetailInfo {
                    LineID = int.Parse(this.txtLineID.Text.Trim()),
                    PutawayRuleID = this.txtPutawayRuleID.Text.Trim(),
                    FromLoc = this.txtFromLoc.Text.Trim(),
                    ToLoc = this.txtToLoc.Text.Trim(),
                    Zone = this.txtZone.Text.Trim(),
                    DimRestriction01 = this.chbDimRestrictions01.Checked ? "10" : "",
                    DimRestriction02 = this.chbDimRestrictions02.Checked ? "20" : "",
                    DimRestriction03 = this.chbDimRestrictions03.Checked ? "30" : "",
                    DimRestriction04 = this.chbDimRestrictions04.Checked ? "40" : "",
                    DimRestriction05 = this.chbDimRestrictions05.Checked ? "50" : "",
                    DimRestriction06 = this.chbDimRestrictions06.Checked ? "60" : "",
                    LocStateRestriction01 = this.chbLocStateRestriction01.Checked ? "10" : "",
                    LocStateRestriction02 = this.chbLocStateRestriction02.Checked ? "20" : "",
                    LocStateRestriction03 = this.chbLocStateRestriction03.Checked ? "30" : "",
                    LocStateRestriction04 = this.chbLocStateRestriction04.Checked ? "40" : "",
                    LocStateRestriction05 = this.chbLocStateRestriction05.Checked ? "50" : "",
                    IsActive = this.chbIsActive.Checked ? "Y" : "N",
                    LocSelection = this.txtLocSel.Text,
                    PaStrategy = this.ddlStrategies.SelectedValue,
                    CreatedBy = base.UserInfo.UserID
                };
                if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(putawayRuleDetail, "PUTAWAY_RULE_DETAIL")))
                {
                    success = true;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2410_006") + ex.Message;
                return;
            }
            if (success)
            {
                this.lblMsg.Text = this.txtLineID.Text + base.GetGlobalMsgResource("C004");
                this.txtLineID.Text = (int.Parse(this.txtLineID.Text.Trim()) + 10).ToString();
                this.txtFromLoc.Text = "";
                this.txtToLoc.Text = "";
                this.txtZone.Text = "";
                this.chbIsActive.Checked = true;
                this.txtLocSel.Text = "";
            }
        }

        protected void ddlStrategies_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            switch (this.ddlStrategies.SelectedItem.Value)
            {
                case "10":
                    this.txtFromLoc.Enabled = false;
                    this.txtToLoc.Enabled = false;
                    this.txtZone.Enabled = false;
                    this.lblFromLocStar.Visible = false;
                    this.lblToLocStar.Visible = false;
                    this.lblZoneStar.Visible = false;
                    this.imgFromLoc.Visible = false;
                    this.imgToLoc.Visible = false;
                    this.imgZone.Visible = false;
                    break;

                case "20":
                    this.txtFromLoc.Enabled = false;
                    this.txtToLoc.Enabled = false;
                    this.txtZone.Enabled = false;
                    this.lblFromLocStar.Visible = false;
                    this.lblToLocStar.Visible = false;
                    this.lblZoneStar.Visible = false;
                    this.imgFromLoc.Visible = false;
                    this.imgToLoc.Visible = false;
                    this.imgZone.Visible = false;
                    break;

                case "30":
                    this.txtFromLoc.Enabled = false;
                    this.txtToLoc.Enabled = false;
                    this.txtZone.Enabled = true;
                    this.lblFromLocStar.Visible = false;
                    this.lblToLocStar.Visible = false;
                    this.lblZoneStar.Visible = true;
                    this.imgFromLoc.Visible = false;
                    this.imgToLoc.Visible = false;
                    this.imgZone.Visible = true;
                    break;

                case "40":
                    this.txtFromLoc.Enabled = false;
                    this.txtToLoc.Enabled = true;
                    this.txtZone.Enabled = false;
                    this.lblFromLocStar.Visible = false;
                    this.lblToLocStar.Visible = true;
                    this.lblZoneStar.Visible = false;
                    this.imgFromLoc.Visible = false;
                    this.imgToLoc.Visible = true;
                    this.imgZone.Visible = false;
                    break;

                case "50":
                    this.txtFromLoc.Enabled = true;
                    this.txtToLoc.Enabled = true;
                    this.txtZone.Enabled = false;
                    this.lblFromLocStar.Visible = true;
                    this.lblToLocStar.Visible = true;
                    this.lblZoneStar.Visible = false;
                    this.imgFromLoc.Visible = true;
                    this.imgToLoc.Visible = true;
                    this.imgZone.Visible = false;
                    break;

                case "60":
                    this.txtFromLoc.Enabled = true;
                    this.txtToLoc.Enabled = false;
                    this.txtZone.Enabled = true;
                    this.lblFromLocStar.Visible = true;
                    this.lblToLocStar.Visible = false;
                    this.lblZoneStar.Visible = true;
                    this.imgFromLoc.Visible = true;
                    this.imgToLoc.Visible = false;
                    this.imgZone.Visible = true;
                    break;

                case "70":
                    this.txtFromLoc.Enabled = false;
                    this.txtToLoc.Enabled = false;
                    this.txtZone.Enabled = false;
                    this.lblFromLocStar.Visible = false;
                    this.lblToLocStar.Visible = false;
                    this.lblZoneStar.Visible = false;
                    this.imgFromLoc.Visible = false;
                    this.imgToLoc.Visible = false;
                    this.imgZone.Visible = false;
                    break;

                case "80":
                    this.txtFromLoc.Enabled = false;
                    this.txtToLoc.Enabled = false;
                    this.txtZone.Enabled = false;
                    this.lblFromLocStar.Visible = false;
                    this.lblToLocStar.Visible = false;
                    this.lblZoneStar.Visible = false;
                    this.imgFromLoc.Visible = false;
                    this.imgToLoc.Visible = false;
                    this.imgZone.Visible = false;
                    break;
            }
            this.txtFromLoc.Text = "";
            this.txtToLoc.Text = "";
            this.txtZone.Text = "";
        }

        private void InitLocSel(DropDownList ddl)
        {
            if (this.sysLocSel == null)
            {
                this.sysLocSel = CommoneMethodServiceClientProxy.GetInstance().GetSysCodeDataSource("Loc Sel");
            }
            ddl.DataSource = this.sysLocSel;
            ddl.DataTextField = "SysCodeText";
            ddl.DataValueField = "SysCodeValue";
            ddl.DataBind();
        }

        private void InitLocStateDDL(CheckBox chkbox, string seqno)
        {
            if (this.sysCodeLocState == null)
            {
                this.sysCodeLocState = CommoneMethodServiceClientProxy.GetInstance().GetSysCodeDataSource("PTLocStateRest");
            }
            for (int i = 0; i < this.sysCodeLocState.Count; i++)
            {
                if (this.sysCodeLocState[i].SysCodeValue == seqno)
                {
                    chkbox.Text = this.sysCodeLocState[i].SysCodeText;
                    break;
                }
            }
        }

        private void InitPTDimRestrictions(CheckBox chkbox, string seqno)
        {
            if (this.sysPTDimRestrictions == null)
            {
                this.sysPTDimRestrictions = CommoneMethodServiceClientProxy.GetInstance().GetSysCodeDataSource("PTDimRestrictions");
            }
            for (int i = 0; i < this.sysPTDimRestrictions.Count; i++)
            {
                if (this.sysPTDimRestrictions[i].SysCodeValue == seqno)
                {
                    chkbox.Text = this.sysPTDimRestrictions[i].SysCodeText;
                    break;
                }
            }
        }

        private void InitPTStrategies(DropDownList ddl)
        {
            if (this.sysPTStrategies == null)
            {
                this.sysPTStrategies = CommoneMethodServiceClientProxy.GetInstance().GetSysCodeDataSource("PTStrategies");
            }
            ddl.DataSource = this.sysPTStrategies;
            ddl.DataTextField = "SysCodeText";
            ddl.DataValueField = "SysCodeValue";
            ddl.DataBind();
        }

        private void InitPutawayRuleDetailDropDownLit()
        {
            this.InitPTStrategies(this.ddlStrategies);
            this.ddlStrategies_SelectedIndexChanged(null, null);
            this.InitLocStateDDL(this.chbLocStateRestriction01, "10");
            this.InitLocStateDDL(this.chbLocStateRestriction02, "20");
            this.InitLocStateDDL(this.chbLocStateRestriction03, "30");
            this.InitLocStateDDL(this.chbLocStateRestriction04, "40");
            this.InitLocStateDDL(this.chbLocStateRestriction05, "50");
            this.InitPTDimRestrictions(this.chbDimRestrictions01, "10");
            this.InitPTDimRestrictions(this.chbDimRestrictions02, "20");
            this.InitPTDimRestrictions(this.chbDimRestrictions03, "30");
            this.InitPTDimRestrictions(this.chbDimRestrictions04, "40");
            this.InitPTDimRestrictions(this.chbDimRestrictions05, "50");
            this.InitPTDimRestrictions(this.chbDimRestrictions06, "60");
        }

        private void LoadData()
        {
            this.InitPutawayRuleDetailDropDownLit();
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"].ToString()))
            {
                string putawayRuleID = base.Request.QueryString["ID"].ToString();
                this.txtPutawayRuleID.Text = putawayRuleID;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"].ToString()))
            {
                string from = base.Request.QueryString["from"].ToString();
                this.hdnFrom.Value = from;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["SeqNO"].ToString()))
            {
                string LineID = base.Request.QueryString["SeqNO"].ToString();
                this.txtLineID.Text = LineID;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["type"]) && (base.Request.QueryString["type"].ToString() == "edit"))
            {
                this.btnSaveContinue.Enabled = false;
                this.hdntype.Value = base.Request.QueryString["type"].ToString();
                this.SetPutawayRuleDetail2Form();
            }
            this.chbDimRestrictions01.Checked = false;
            this.chbLocStateRestriction01.Checked = false;
            this.chbLocStateRestriction02.Attributes.Add("onclick", "chkClick(this,'" + this.chbLocStateRestriction03.ClientID + "');");
            this.chbLocStateRestriction03.Attributes.Add("onclick", "chkClick(this,'" + this.chbLocStateRestriction02.ClientID + "');");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetPutawayRuleDetail2Form()
        {
            if ((this.txtPutawayRuleID.Text.Trim() != "") && (this.txtLineID.Text.Trim() != ""))
            {
                PutawayRuleDetailInfo putawayRuleDetailInfo = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(this.txtPutawayRuleID.Text.Trim(), this.txtLineID.Text.Trim(), "", "PUTAWAY_RULE_DETAIL") as PutawayRuleDetailInfo;
                if (putawayRuleDetailInfo != null)
                {
                    this.chbDimRestrictions01.Checked = !string.IsNullOrEmpty(putawayRuleDetailInfo.DimRestriction01);
                    this.chbDimRestrictions02.Checked = !string.IsNullOrEmpty(putawayRuleDetailInfo.DimRestriction02);
                    this.chbDimRestrictions03.Checked = !string.IsNullOrEmpty(putawayRuleDetailInfo.DimRestriction03);
                    this.chbDimRestrictions04.Checked = !string.IsNullOrEmpty(putawayRuleDetailInfo.DimRestriction04);
                    this.chbDimRestrictions05.Checked = !string.IsNullOrEmpty(putawayRuleDetailInfo.DimRestriction05);
                    this.chbDimRestrictions06.Checked = !string.IsNullOrEmpty(putawayRuleDetailInfo.DimRestriction06);
                    this.chbLocStateRestriction01.Checked = !string.IsNullOrEmpty(putawayRuleDetailInfo.LocStateRestriction01);
                    this.chbLocStateRestriction02.Checked = !string.IsNullOrEmpty(putawayRuleDetailInfo.LocStateRestriction02);
                    this.chbLocStateRestriction03.Checked = !string.IsNullOrEmpty(putawayRuleDetailInfo.LocStateRestriction03);
                    this.chbLocStateRestriction04.Checked = !string.IsNullOrEmpty(putawayRuleDetailInfo.LocStateRestriction04);
                    this.chbLocStateRestriction05.Checked = !string.IsNullOrEmpty(putawayRuleDetailInfo.LocStateRestriction05);
                    this.txtLocSel.Text = putawayRuleDetailInfo.LocSelection;
                    this.ddlStrategies.SelectedValue = putawayRuleDetailInfo.PaStrategy;
                    this.ddlStrategies_SelectedIndexChanged(null, null);
                    this.chbIsActive.Checked = putawayRuleDetailInfo.IsActive == "Y";
                    this.txtFromLoc.Text = putawayRuleDetailInfo.FromLoc;
                    this.txtToLoc.Text = putawayRuleDetailInfo.ToLoc;
                    this.txtZone.Text = putawayRuleDetailInfo.Zone;
                }
            }
        }
    }
}

